﻿using System.Web.Mvc;
using Lyh.Common.Face;
using Lyh.Core.Result;
using Lyh.Models;
using Lyh.Models.Ext;

namespace Lyh.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login(Login model)
        {
            var result = new ExtResult();
            if (ModelState.IsValid)
            {
                if (Session["Code"] != null && model.Code.ToLower() == Session["Code"].ToString().ToLower())
                {

                    result.Success = true;
                    result.Title = "登录";
                    result.Message = "恭喜";
                }
                else
                {
                    result.Success = false;
                    result.Title = "验证码";
                    result.Message = "验证码错误";
                }
            }
            else
            {
                result.Success = false;
                result.Title = "登录";
                result.Message = "输入的信息有误";
            }
            return this.CustomJson(result, false, true);
        }

        public ActionResult Code()
        {
            var v = new Verification();
            var code = v.CreateVerifyCode();  
            Session["Code"] = code;
            var bytes = v.CreateImage(code);
            return File(bytes, @"image/jpeg");
        }
    }
}
